<template>
  <div class="dashboard-container">
    <div class="app-container">
      <upload-excel :beforeUpload="beforeUpload" :onSuccess="successFn" />
    </div>
  </div>
</template>

<script>
import EmployeesEnum from '@/constant/employees'
import { importEmployee } from '@/api/employees'
import { parseTime } from '@/utils'
const { mapKeyPath } = EmployeesEnum
export default {
  methods: {
    beforeUpload(file) {
      if (file.name.endsWith(".xlsx")) {
        return true;
      }
      return alert("请选择excel文件");
    },
    async successFn({ header, results }) {
      const res = results.map(item => {
        const obj = {}
        for (let key in item) {
            // 以 key为 入职日期 为例
            if (key === '入职日期' || key === '转正日期') {
                const timesteap = item[key]
                // 我们首先减去一天，然后把天数转化为毫秒数
                const date = new Date((timesteap - 1) * 24 * 3600000)
                // 然后把得到的时间再减去70年
                const time = parseTime(date.setFullYear(date.getFullYear() - 70), '{y}-{m}-{d}')
                obj[mapKeyPath[key]] = time
            } else {
                obj[mapKeyPath[key]] = item[key]
            }
            
        }
        return obj
      })
      await importEmployee(res)
      this.$message.success('导入成功')
      this.$router.go(-1)
    },
  },
};
</script>

<style>
</style>